package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.FileReissue;
import com.amsp.oa.oademo.entity.FileReissueDetail;
import com.amsp.oa.oademo.mapper.FileReissueDetailMapper;
import com.amsp.oa.oademo.mapper.FileReissueMapper;
import com.amsp.oa.oademo.service.FileReissueService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class FileReissueServiceImpl extends ServiceImpl<FileReissueMapper, FileReissue> implements FileReissueService {

    @Autowired
    private FileReissueDetailMapper detailMapper;

    @Override
    public IPage<FileReissue> getPage(Integer page, Integer size, String keyword) {
        Page<FileReissue> pageParam = new Page<>(page, size);
        LambdaQueryWrapper<FileReissue> wrapper = new LambdaQueryWrapper<>();

        if (StringUtils.isNotBlank(keyword)) {
            wrapper.like(FileReissue::getApplicationUnit, keyword)
                    .or()
                    .exists("SELECT 1 FROM t_file_reissue_detail d WHERE d.reissue_id = t_file_reissue.id AND " +
                            "(d.file_number LIKE CONCAT('%', {0}, '%') OR d.file_name LIKE CONCAT('%', {0}, '%'))",
                            keyword);
        }

        wrapper.orderByDesc(FileReissue::getCreateTime);
        return baseMapper.selectPage(pageParam, wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public FileReissue saveOrUpdateWithDetails(FileReissue fileReissue) {
        // 保存主表
        saveOrUpdate(fileReissue);

        // 删除原有明细
        if (fileReissue.getId() != null) {
            detailMapper.delete(new LambdaQueryWrapper<FileReissueDetail>()
                    .eq(FileReissueDetail::getReissueId, fileReissue.getId()));
        }

        // 保存新的明细
        if (fileReissue.getDetails() != null && !fileReissue.getDetails().isEmpty()) {
            for (FileReissueDetail detail : fileReissue.getDetails()) {
                detail.setReissueId(fileReissue.getId());
                detailMapper.insert(detail);
            }
        }

        return getByIdWithDetails(fileReissue.getId());
    }

    @Override
    public FileReissue getByIdWithDetails(Long id) {
        FileReissue fileReissue = getById(id);
        if (fileReissue != null) {
            List<FileReissueDetail> details = detailMapper.selectList(
                    new LambdaQueryWrapper<FileReissueDetail>()
                            .eq(FileReissueDetail::getReissueId, id));
            fileReissue.setDetails(details);
        }
        return fileReissue;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void removeWithDetails(Long id) {
        // 删除明细
        detailMapper.delete(new LambdaQueryWrapper<FileReissueDetail>()
                .eq(FileReissueDetail::getReissueId, id));
        // 删除主表
        removeById(id);
    }
}